﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder
{
    /// <summary>
    /// 费用来源——comment by Webb.Wen
    /// </summary>
    public class B1Form_CreateVoucherList : SAPBoAddon.B1Form
    {
        int FeeDocEntry = 0;
        public B1Form_CreateVoucherList()
        {
            frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_CreateVoucherList_frm_ItemEvent);
        }
        #region 继承自父类
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_CreateVoucherList;
            FormSetting.FormFileName = "AVA_FF_CreateVoucherListForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            SAPBoAddon.B1Assistant.Item.SetValue(CurrentForm, "12", "Y");/*默认选中 "应收"*/

            if (InitPar.FatherFormUID.StartsWith(B1FormTypes.ado_AVA_FF_CreateCost))
            {
                this.CurrentForm.DataSources.UserDataSources.Item("ParentUID").Value = InitPar.FatherFormUID;
                FeeDocEntry = Convert.ToInt32(InitPar.KeyValue);
                this.ShowOpenCompany(this.CurrentForm.UniqueID);
            }
        }
        #endregion
        void B1Form_CreateVoucherList_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {

            }
            else
            {
                if (pVal.ItemUID == "SelectBP" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    this.LoadVoucher(pVal.ItemUID);
                }
                if (pVal.ItemUID == "SelectFees" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    this.LoadVoucher(pVal.ItemUID);
                }
                if (pVal.ItemUID == "FeeList" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    this.ShowOpenFeeItems();
                }
                if ((pVal.ItemUID == "11" || pVal.ItemUID == "12" || pVal.ItemUID == "13") && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    this.ShowOpenCompany(pVal.FormUID);
                }
                if (pVal.ItemUID == "CloseGrid" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_CLICK && pVal.Row >= 0 && pVal.ColUID != "RowsHeader")
                {
                    SAPbouiCOM.Grid oGrid = this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                    SAPbouiCOM.RowHeaders oRh = oGrid.RowHeaders;
                    oGrid.Columns.Item("RowsHeader").Click(pVal.Row);
                }
                if (pVal.ItemUID == "Prov" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    this.CurrentForm.PaneLevel = 1;
                }
            }
        }
        #region 私有函数
        private void ShowOpenCompany(string FormUID)
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.Grid oCloseGrid;
            SAPbouiCOM.DataTable Closedt = default(SAPbouiCOM.DataTable);
            int DocEntry;
            string BillType = string.Empty;
            string SQL_Dt = string.Empty;
            const string dtUID = "CloseCustoms";/*DataTable UID*/
            const string ItemUID_BillType_All = "11";
            const string ItemUID_BillType_Debit = "12";
            const string ItemUID_BillType_Credit = "13";
            string Condition_BillType = string.Empty;

            oForm = SBOApp.Forms.Item(FormUID);
            oForm.Freeze(true);
            DocEntry = FeeDocEntry;

            oCloseGrid = oForm.Items.Item("CloseGrid").Specific;
            if (SAPBoAddon.B1Assistant.Item.GetValue(oForm, ItemUID_BillType_All).ToString() == "Y")
            {
                BillType = string.Empty;
            }
            else if (SAPBoAddon.B1Assistant.Item.GetValue(oForm, ItemUID_BillType_Debit).ToString() == "Y")
            {
                BillType = "I";
            }
            else if (SAPBoAddon.B1Assistant.Item.GetValue(oForm, ItemUID_BillType_Credit).ToString() == "Y")
            {
                BillType = "O";
            }
            if (!string.IsNullOrEmpty(BillType))
            {
                Condition_BillType = string.Format("And U_BillType =N'{0}'", BillType);
            }
            /*添加DataTable 考虑放在srf文件中*/
            for (int DtIndex = 0; DtIndex < oForm.DataSources.DataTables.Count; DtIndex++)
            {
                if (dtUID == oForm.DataSources.DataTables.Item(DtIndex).UniqueID)
                {
                    Closedt = oForm.DataSources.DataTables.Item(dtUID);
                    break;
                }
            }
            if (Closedt == null)
            {
                Closedt = oForm.DataSources.DataTables.Add(dtUID);
            }

            Closedt.Clear();
            SQL_Dt = string.Format(" SELECT Distinct U_CardName,U_CardCode FROM [@AVA_FF_CCTV] Where  ISNULL(U_CardCode,N'N')<>N'N' And ISNULL(U_VoucherEntry,N'N')=N'N' And DocEntry ={0} {1}  AND U_Status=N'P' Order By U_CardName ", DocEntry, Condition_BillType);
            Closedt.ExecuteQuery(SQL_Dt);
            oCloseGrid.DataTable = Closedt;
            oCloseGrid.Columns.Item("U_CardName").TitleObject.Caption = "结算单位名称";
            oCloseGrid.Columns.Item("U_CardCode").TitleObject.Caption = "结算单位";
            oCloseGrid.Columns.Item("U_CardCode").Visible = true;
            oCloseGrid.AutoResizeColumns();
            oCloseGrid.RowHeaders.Width = 20;
            oForm.Items.Item("CloseGrid").Enabled = false;

            string Cardcode_FirstLine = Closedt.GetValue("U_CardCode", 0);
            /*默认选中第一行*/
            if (oCloseGrid.Rows.Count >= 1 && !string.IsNullOrEmpty(Cardcode_FirstLine))
            {
                oCloseGrid.Rows.SelectedRows.Add(0);
            }
            oForm.Freeze(false);
        }

        private void ShowOpenFeeItems()
        {
            SAPbouiCOM.Grid Grid_OpenFee;
            SAPbouiCOM.DataTable Dt_OpenFee = default(SAPbouiCOM.DataTable);
            int DocEntry;
            string SQL_Dt = string.Empty;
            const string dtUID = "Dt_OpenFee";/*DataTable UID*/
            const string ItemUID_BillType_All = "11";
            const string ItemUID_BillType_Debit = "12";
            const string ItemUID_BillType_Credit = "13";
            string DataTableName = string.Empty;
            string CardCode = string.Empty;

            DocEntry = FeeDocEntry;
            SAPbouiCOM.DataTable Dt_OpenBP = this.CurrentForm.DataSources.DataTables.Item("CloseCustoms");
            SAPbouiCOM.Grid Grid_OpenBP = this.CurrentForm.Items.Item("CloseGrid").Specific;

            if (Grid_OpenBP.Rows.SelectedRows.Count > 0)
            {
                int SelectLine = Grid_OpenBP.Rows.SelectedRows.Item(0, SAPbouiCOM.BoOrderType.ot_RowOrder);
                CardCode = Dt_OpenBP.GetValue("U_CardCode", SelectLine);
            }
            else
            {
                SBOApp.StatusBar.SetText("请选中一结算单位.", SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                return;
            }
            Grid_OpenFee = CurrentForm.Items.Item("OpenFee").Specific;
            if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, ItemUID_BillType_All).ToString() == "Y")
            {
                DataTableName = "[@AVA_FF_CCTV] ";
            }
            else if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, ItemUID_BillType_Debit).ToString() == "Y")
            {
                DataTableName = "[@AVA_FF_CCT1] ";
            }
            else if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, ItemUID_BillType_Credit).ToString() == "Y")
            {
                DataTableName = "[@AVA_FF_CCT2] ";
            }
            /*添加DataTable 考虑放在srf文件中*/
            for (int DtIndex = 0; DtIndex < CurrentForm.DataSources.DataTables.Count; DtIndex++)
            {
                if (dtUID == CurrentForm.DataSources.DataTables.Item(DtIndex).UniqueID)
                {
                    Dt_OpenFee = CurrentForm.DataSources.DataTables.Item(dtUID);
                    break;
                }
            }
            if (Dt_OpenFee == null)
            {
                Dt_OpenFee = CurrentForm.DataSources.DataTables.Add(dtUID);
            }

            Dt_OpenFee.Clear();
            SQL_Dt = string.Format(" SELECT LineId,U_CostCode,U_CostName,U_CostMoney FROM {0} WHERE U_CardCode=N'{1}' " +
                "And ISNULL(U_VoucherEntry,N'N')=N'N' And DocEntry ={2}  AND U_Status=N'P' ", DataTableName, CardCode, DocEntry);
            Dt_OpenFee.ExecuteQuery(SQL_Dt);
            Grid_OpenFee.DataTable = Dt_OpenFee;
            Grid_OpenFee.Columns.Item("U_CostCode").Visible = false;
            Grid_OpenFee.Columns.Item("U_CostName").TitleObject.Caption = "费用";
            Grid_OpenFee.Columns.Item("U_CostName").Width = 89;
            Grid_OpenFee.Columns.Item("U_CostMoney").TitleObject.Caption = "金额";
            Grid_OpenFee.Columns.Item("U_CostName").Width = 43;
            Grid_OpenFee.AutoResizeColumns();
            CurrentForm.Items.Item("OpenFee").Enabled = false;
            Grid_OpenFee.SelectionMode = SAPbouiCOM.BoMatrixSelect.ms_Auto;
            CurrentForm.PaneLevel = 2;
        }

        private void AddCloseCompany(string FormUID)
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.Grid oCloseGrid;
            SAPbouiCOM.DataTable Closedt = default(SAPbouiCOM.DataTable);
            string BillType = string.Empty;
            string SQL_Dt = string.Empty;
            const string dtUID = "CloseCustoms";/*DataTable UID*/
            const string ItemUID_BillType_All = "11";
            const string ItemUID_BillType_Debit = "12";
            const string ItemUID_BillType_Credit = "13";
            string Condition_BillType = string.Empty;

            oForm = SBOApp.Forms.Item(FormUID);
            oForm.Freeze(true);

            oCloseGrid = oForm.Items.Item("CloseGrid").Specific;

            if (SAPBoAddon.B1Assistant.Item.GetValue(oForm, ItemUID_BillType_All).ToString() == "Y")
            {
                BillType = string.Empty;
            }
            else if (SAPBoAddon.B1Assistant.Item.GetValue(oForm, ItemUID_BillType_Debit).ToString() == "Y")
            {
                BillType = "I";
            }
            else if (SAPBoAddon.B1Assistant.Item.GetValue(oForm, ItemUID_BillType_Credit).ToString() == "Y")
            {
                BillType = "O";
            }
            if (!string.IsNullOrEmpty(BillType))
            {
                Condition_BillType = string.Format("And U_BillType =N'{0}'", BillType);
            }
            /*添加DataTable 考虑放在srf文件中*/
            if (oForm.DataSources.DataTables.Count == 0)
            {
                Closedt = oForm.DataSources.DataTables.Add(dtUID);
            }
            else
            {
                for (int DtIndex = 0; DtIndex < oForm.DataSources.DataTables.Count; DtIndex++)
                {
                    if (dtUID == oForm.DataSources.DataTables.Item(DtIndex).UniqueID)
                    {
                        Closedt = oForm.DataSources.DataTables.Item(dtUID);
                        break;
                    }
                    if (DtIndex == oForm.DataSources.DataTables.Count - 1)
                    {
                        Closedt = oForm.DataSources.DataTables.Add(dtUID);
                        break;
                    }
                }
            }
            Closedt.Clear();
            SQL_Dt = string.Format(" SELECT Distinct U_CardName,U_CardCode FROM [@AVA_FF_CCTV] Where ISNULL(U_VoucherEntry,N'N')=N'N' And DocEntry ={0} {1}  AND U_Status=N'P' Order By U_CardName ", FeeDocEntry, Condition_BillType);
            Closedt.ExecuteQuery(SQL_Dt);
            oCloseGrid.DataTable = Closedt;
            oCloseGrid.Columns.Item("U_CardName").TitleObject.Caption = "结算单位名称";
            oCloseGrid.Columns.Item("U_CardCode").TitleObject.Caption = "结算单位";
            oCloseGrid.Columns.Item("U_CardCode").Visible = true;
            oCloseGrid.AutoResizeColumns();
            oCloseGrid.RowHeaders.Width = 20;
            oForm.Items.Item("CloseGrid").Enabled = false;
            /*默认选中第一行*/
            if (oCloseGrid.Rows.Count >= 1)
            {
                oCloseGrid.Rows.SelectedRows.Add(0);
            }

            oForm.Freeze(false);
        }

        private void LoadVoucher(string ItemUID)
        {
            SAPbouiCOM.Grid oGrid_OpenBp = CurrentForm.Items.Item("CloseGrid").Specific;
            SAPbouiCOM.Grid oGrid_OpenFee = CurrentForm.Items.Item("OpenFee").Specific;
            //SAPbouiCOM.DataTable Dt_OpenFee = this.CurrentForm.DataSources.DataTables.Item("Dt_OpenFee");
            SAPbouiCOM.DataTable Dt_OpenFee = CreateFeeItemDataTable();
            string StrLines = string.Empty;
            string SQL_LineIdIn = string.Empty;
            if (CurrentForm.PaneLevel == 1)
            {
                if (oGrid_OpenBp.Rows.SelectedRows.Count == 0)
                {
                    SBOApp.StatusBar.SetText("请选择结算单位行...", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                    return;
                }
            }
            else
            {
                if (oGrid_OpenFee.Rows.SelectedRows.Count == 0)
                {
                    SBOApp.StatusBar.SetText("请选择费用行...", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                    return;
                }
                int SelectLine = 0;
                for (int i = 0; i < oGrid_OpenFee.Rows.SelectedRows.Count; i++)
                {
                    SelectLine = oGrid_OpenFee.Rows.SelectedRows.Item(i, SAPbouiCOM.BoOrderType.ot_RowOrder);
                    StrLines += Dt_OpenFee.GetValue("LineId", SelectLine) + ",";
                }
                SQL_LineIdIn = string.Format(" AND LineId In({0}) ", StrLines.Remove(StrLines.Length - 1));
            }

            SAPBoAddon.B1FormInitializePar oInitipar = new SAPBoAddon.B1FormInitializePar();
            oInitipar.FatherFormUID = this.CurrentForm.UniqueID;
            oInitipar.KeyValue = FeeDocEntry.ToString();
            oInitipar.parObject = SQL_LineIdIn;
            this.ShowForm(B1FormTypes.ado_AVA_FF_CreateVoucher, oInitipar);
        }
        /// <summary>
        /// 创建费用项DataTable
        /// </summary>
        /// <returns></returns>
        private SAPbouiCOM.DataTable CreateFeeItemDataTable()
        {
            SAPbouiCOM.DataTable Dt_OpenFee = default(SAPbouiCOM.DataTable);
            const string dtUID = "Dt_OpenFee";/*DataTable UID*/
            /*添加DataTable 考虑放在srf文件中*/
            for (int DtIndex = 0; DtIndex < CurrentForm.DataSources.DataTables.Count; DtIndex++)
            {
                if (dtUID == CurrentForm.DataSources.DataTables.Item(DtIndex).UniqueID)
                {
                    Dt_OpenFee = CurrentForm.DataSources.DataTables.Item(dtUID);
                    break;
                }
            }
            if (Dt_OpenFee == null)
            {
                Dt_OpenFee = CurrentForm.DataSources.DataTables.Add(dtUID);
            }
            return Dt_OpenFee;
        }
        #endregion


    }
}
